from django.http import JsonResponse, HttpRequest
from django.views import View
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
import json
from typing import Dict, Any


# In-memory stores (prototype only)
CANDIDATES: Dict[int, Dict[str, Any]] = {}
INTERVIEWS: Dict[int, Dict[str, Any]] = {}
_next_candidate_id = 1
_next_interview_id = 1


def _json_body(request: HttpRequest) -> Dict[str, Any]:
    try:
        return json.loads(request.body.decode("utf-8") or "{}")
    except json.JSONDecodeError:
        return {}


class HealthView(View):
    def get(self, request):
        return JsonResponse({"status": "ok", "app": "interview"})


@method_decorator(csrf_exempt, name='dispatch')
class CandidateListView(View):
    def get(self, request: HttpRequest):
        return JsonResponse({"items": list(CANDIDATES.values())})

    def post(self, request: HttpRequest):
        global _next_candidate_id
        data = _json_body(request)
        name = data.get("name", "").strip()
        email = data.get("email", "").strip()
        phone = data.get("phone", "").strip()
        if not name or not email:
            return JsonResponse({"error": "name and email are required"}, status=400)
        cid = _next_candidate_id
        _next_candidate_id += 1
        CANDIDATES[cid] = {
            "id": cid,
            "name": name,
            "email": email,
            "phone": phone,
        }
        return JsonResponse(CANDIDATES[cid], status=201)


@method_decorator(csrf_exempt, name='dispatch')
class InterviewListView(View):
    def get(self, request: HttpRequest):
        return JsonResponse({"items": list(INTERVIEWS.values())})

    def post(self, request: HttpRequest):
        global _next_interview_id
        data = _json_body(request)
        candidate_id = data.get("candidate_id")
        scheduled_at = data.get("scheduled_at", "")  # ISO string for prototype
        interviewer = data.get("interviewer", "").strip()
        location = data.get("location", "").strip()
        result = data.get("result", "").strip()
        feedback = data.get("feedback", "").strip()

        if not candidate_id or candidate_id not in CANDIDATES:
            return JsonResponse({"error": "valid candidate_id is required"}, status=400)
        if not scheduled_at or not interviewer:
            return JsonResponse({"error": "scheduled_at and interviewer are required"}, status=400)

        iid = _next_interview_id
        _next_interview_id += 1
        INTERVIEWS[iid] = {
            "id": iid,
            "candidate_id": candidate_id,
            "scheduled_at": scheduled_at,
            "interviewer": interviewer,
            "location": location,
            "result": result,
            "feedback": feedback,
        }
        return JsonResponse(INTERVIEWS[iid], status=201)


@method_decorator(csrf_exempt, name='dispatch')
class ResetView(View):
    def post(self, request: HttpRequest):
        global _next_candidate_id, _next_interview_id
        CANDIDATES.clear()
        INTERVIEWS.clear()
        _next_candidate_id = 1
        _next_interview_id = 1
        return JsonResponse({"status": "reset"})
